package com.yz.android_baozou.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yz.android_baozou.R;
import com.yz.android_baozou.bean.VideoBean;
import com.yz.android_baozou.helper.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */

public class Home_Video_RecyclerAdapter extends RecyclerViewAdapterHelper<VideoBean.DataBean> {
    public Home_Video_RecyclerAdapter(Context context, List<VideoBean.DataBean> list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_video_home, parent, false);
        return new videoViewHolder(view);
    }


    @Override
    public void onBindMyViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof videoViewHolder) {
            Glide.with(context).load(list.get(position).getImage()).into(((videoViewHolder) holder).imageView_img_videoitem);
            ((videoViewHolder) holder).textView_title_videoitem.setText(list.get(position).getTitle());
            ((videoViewHolder) holder).textView_Playcounts_videoitem.setText(list.get(position).getPlay_count() + "");
            ((videoViewHolder) holder).imageview_pause_videoitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //给video view设置控制器
                    ((videoViewHolder) holder).actVideoview.setMediaController(new MediaController(context));
                    //设置视屏播放地址,并且缓存
                    ((videoViewHolder) holder).actVideoview.setVideoURI(Uri.parse(list.get(position).getFile_url()));
                    //让videoview获得焦点
                    ((videoViewHolder) holder).actVideoview.requestFocus();
                    //开始播放
                    ((videoViewHolder) holder).imageView_img_videoitem.setVisibility(View.INVISIBLE);
                    ((videoViewHolder) holder).imageview_pause_videoitem.setVisibility(View.INVISIBLE);
                    ((videoViewHolder) holder).actVideoview.setVisibility(View.VISIBLE);
                    ((videoViewHolder) holder).actVideoview.start();
                }
            });
        }
    }

    public VideoBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<VideoBean>() {
        }.getType());
    }

    class videoViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView_fileurl_videoitem, imageView_img_videoitem, imageview_pause_videoitem;
        private TextView textView_title_videoitem, textView_Playcounts_videoitem;
        private VideoView actVideoview;

        public videoViewHolder(View itemView) {
            super(itemView);
            imageView_img_videoitem = (ImageView) itemView.findViewById(R.id.imageView_img_videoitem);
            textView_title_videoitem = (TextView) itemView.findViewById(R.id.textView_title_videoitem);
            textView_Playcounts_videoitem = (TextView) itemView.findViewById(R.id.textView_Playcounts_videoitem);
            imageview_pause_videoitem = (ImageView) itemView.findViewById(R.id.imageview_pause_videoitem);
            actVideoview = (VideoView) itemView.findViewById(R.id.actVideoview);
        }
    }
}
